package com.dream.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.dream.able.SmallNewsMapper;
import com.dream.datasource.DBContextHolder;
import com.dream.pojo.SmallNews;
import com.dream.pojo.SmallNewsExample;
import com.dream.service.able.All_Service_able;
import com.dream.service.able.Limit_able;


/*
 * 小消息业务类
 */
@Service("Small_News_Servicimpl")
public class Small_News_Servicimpl 
implements All_Service_able<SmallNews>,
Limit_able<SmallNews>{

	/*
	 * 首页小消息表的DAO代理对象
	 */
	@Resource(name="smallNewsMapper")
	private SmallNewsMapper samll_news_mapper;

	/*
	 * (添加一条小消息,传入消息内容)
	 * @see com.dream.service.able.All_Service_able#insert(java.lang.String[])
	 */
	@Override
	public SmallNews insert(String... str) {
		//切换数据源，主库
		DBContextHolder.setDbType(DBContextHolder.master);
		//创建消息对象
		SmallNews smallNews = new SmallNews();
		//插入消息内容
		smallNews.setContext(str[0]);
		//插入状态
		smallNews.setState(1);
		//写入数据库
		return samll_news_mapper.insertSelective(smallNews)>0?smallNews:null;
	}

	/*
	 * (查询有效的消息集)
	 * @see com.dream.service.able.All_Service_able#list()
	 */
	@Override
	public List<SmallNews> list() {
		//切换数据库，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		SmallNewsExample example = new SmallNewsExample();
		//设置排序规则
		example.setOrderByClause("sid desc");
		//创建查询条件工具内部类
		SmallNewsExample.Criteria criteria = example.createCriteria();
		//设置查询条件，state=1
		criteria.andStateEqualTo(1);
		//返回查询到数据集
		return samll_news_mapper.selectByExample(example);
	}

	@Override
	public SmallNews getT(String... str) {
		// TODO Auto-generated method stub
		return null;
	}
	/*
	 * (查询指定条数和位置的数据,传入开始的位置，和返回的数据数目)
	 * @see com.dream.service.able.Limit_able#list_Limit(java.lang.String[])
	 */
	@Override
	public List<SmallNews> list_Limit(String... str) {
		//切换数据库，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		SmallNewsExample example = new SmallNewsExample();
		//设置排序规则
		example.setOrderByClause("sid desc");
		//设置查询的开始位置
		example.setOffset(Integer.parseInt(str[0]));
		//设置返回的数目
		example.setLimit(Integer.parseInt(str[1]));
		//创建查询条件工具内部类
		SmallNewsExample.Criteria criteria = example.createCriteria();
		//设置查询条件，state=1
		criteria.andStateEqualTo(1);
		//返回查询到数据集
		return samll_news_mapper.selectByExample(example);
	}
}
